package com.rem.springboot2.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rem
 * @date 2021-03-19
 */

@RestController
public class My3AnnotationController {


    /**
     * -@PathVariable 测试
     * 请求：http://localhost:8888/car/2/owner/张三
     * 返回:{"pv":{"id":"2","username":"张三"},"name":"张三","id":2}
     *
     * @param id
     * @param name
     * @param pv
     * @return
     */
    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String name,
                                      @PathVariable Map<String, String> pv) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("pv", pv);
        return map;
    }

    /**
     * -@RequestHeader 测试
     * 请求：http://localhost:8888/car2
     *
     * @param userAgent
     * @param header
     * @return
     */
    @GetMapping("/car2")
    public Map<String, Object> getCar2(@RequestHeader("User-Agent") String userAgent,
                                       @RequestHeader Map<String, String> header) {
        Map<String, Object> map = new HashMap<>();
        map.put("userAgent", userAgent);
        map.put("header", header);
        return map;
    }


    /**
     * -@RequestParam 测试
     * 请求：http://localhost:8888/car3?age=18&inters=english&inters=math
     *
     * @param age
     * @param inters
     * @param params
     * @return
     */
    @GetMapping("/car3")
    public Map<String, Object> getCar(@RequestParam("age") Integer age,
                                      @RequestParam("inters") List<String> inters,
                                      @RequestParam Map<String, String> params) {
        Map<String, Object> map = new HashMap<>();
        map.put("age", age);
        map.put("inters", inters);
        map.put("params", params);
        return map;
    }

    /**
     * -@CookieValue 测试
     *
     * @param _ga
     * @param cookie
     * @return
     */
    @GetMapping("/car4")
    public Map<String, Object> getCar(@CookieValue("_ga") String _ga,
                                      @CookieValue("_ga") Cookie cookie) {
        Map<String, Object> map = new HashMap<>();
        map.put("_ga", _ga);
        System.out.println(cookie.getName() + "===>" + cookie.getValue());
        return map;
    }

    /**
     * post请求 保存
     *
     * @param content
     * @return
     */
    @PostMapping("/save")
    public Map postMethod(@RequestBody String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        System.out.println(map);
        return map;
    }


    /**
     * 1、语法： 请求路径：http://localhost:8888/cars/low=34;brand=byd,audi,yd
     * 2、SpringBoot默认是禁用了矩阵变量的功能 手动开启：原理。对于路径的处理。UrlPathHelper进行解析。
     * removeSemicolonContent（移除分号内容）支持矩阵变量的
     * 3、矩阵变量必须有url路径变量才能被解析
     *
     * @param low
     * @param brand
     * @param path
     * @return
     */
    @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path) {
        Map<String, Object> map = new HashMap<>();

        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);
        return map;
    }

}
